
#include "stdafx.h"

#include "vld.h"

#include "BaseApp.h"
#include "IntroView.h"
#include "FluidView.h"
#include "GameView.h"
#include "EditorView.h"
#include "ViewManager.h"
#include "GlobalAppData.h"
#include "BrickGameView.h"

class FluidApp : public mcore::BaseApp
{
public:
	FluidApp(HINSTANCE hInstance)
		: BaseApp(hInstance)
	{

	}
	virtual ~FluidApp(){}

	virtual void userInit()
	{
		mfluid::FluidView * view = new mfluid::FluidView;
		//intro::IntroView * view = new intro::IntroView;
		view->init();
		mCore.getViewManager()->setActiveView(view);
	}
};

class TestApp : public mcore::BaseApp
{
public:
	TestApp(HINSTANCE hInstance)
		: BaseApp(hInstance)
	{

	}

	virtual ~TestApp(){}

	virtual LRESULT msgProc(UINT msg, WPARAM wParam, LPARAM lParam)
	{
		switch (msg)
		{
		case WM_KEYDOWN:
			if(wParam == VK_SHIFT)
			{
				if (mEditorActive)
				{
					if(mGameView == NULL)
						mGameView = new game::GameView;
						//mGameView = new game::GameView;

					mEditorView->saveSceneToFile("scene.xml");

					mGameView->init(mEditorView->mScene, mEditorView->mRenderSystem, mEditorView->mPhysicsSystem);
					mEditorView->mScene = NULL;
					mEditorView->mRenderSystem = NULL;
					mEditorView->mPhysicsSystem = NULL;
					mEditorView->unload();

					mCore.getViewManager()->setActiveView(mGameView);

					mcore::GlobalAppData::Instance.IsSimulationRunning = true;
				}
				else
				{
					mGameView->unload();

					mEditorView->loadSceneFromFile("scene.xml");
					mCore.getViewManager()->setActiveView(mEditorView);

					mcore::GlobalAppData::Instance.IsSimulationRunning = false;
				}
				mEditorActive = !mEditorActive;
			}
		}

		return BaseApp::msgProc(msg, wParam, lParam);
	}

	virtual void userInit()
	{
		mEditorView = new medit::EditorView;
		mEditorView->loadSceneFromFile("scene.xml");
		mEditorActive = true;

		mcore::GlobalAppData::Instance.IsSimulationRunning = false;

		mGameView = NULL;
		//mCore.getViewManager()->setActiveView(new intro::IntroView);
		//mCore.getViewManager()->setActiveView(new game::GameView);
		mCore.getViewManager()->setActiveView(mEditorView);
	}

	bool mEditorActive;

	game::GameView * mGameView;
	medit::EditorView * mEditorView;
};

class BricksGame : public mcore::BaseApp
{
public:
	BricksGame(HINSTANCE hInstance)
		: BaseApp(hInstance)
	{

	}

	virtual ~BricksGame(){}

	virtual LRESULT msgProc(UINT msg, WPARAM wParam, LPARAM lParam)
	{
		return BaseApp::msgProc(msg, wParam, lParam);
	}

	virtual void userInit()
	{
		mGameView = new BrickGameView;
		mGameView->loadSceneFromFile("bricks/scene.xml");
		mGameView->startGame();

		mcore::GlobalAppData::Instance.IsSimulationRunning = false;

		mCore.getViewManager()->setActiveView(mGameView);
	}


	BrickGameView * mGameView;
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
//#if defined(DEBUG) | defined(_DEBUG)
//	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
//#endif

	int result;
	{
		//TestApp app(hInstance);
		BricksGame app(hInstance);
		//ProjEditApp app(hInstance);
		//FluidApp app(hInstance);
		app.init();
		result = app.run();
	}

	//_CrtDumpMemoryLeaks();
	
	return result;
}
